/*global angular*/

/**
 * Navigation Controller
 *
 * Manage device and account navigation and account adding.
 *
 * @see  nav.html
 */
angular.module('webwalletApp').controller('NavCtrl', function (
    $scope,
    $location,
    deviceList,
    $translate,
    $log,
    $modal,
    $rootScope,
    flash) {

    'use strict';

    $scope.devices = function () {
        return deviceList.all();
    };

    $scope.isActive = function (path) {
        return $location.path().match(path);
    };

    $scope.addingInProgress = false;

    $scope.addAccount = function (dev) {
        $scope.addingInProgress = true;
        dev.addAccount().then(
            function (acc) {
                $location.path('/device/' + dev.id + '/account/' + acc.id);
                $scope.addingInProgress = false;
            },
            function (err) {
                flash.error(err.message || $translate.instant('js.controllers.nav.add-account-failed'));
            }
        );
    };

    $scope.accountLink = function (dev, acc) {
        var link = '#/device/' + dev.id + '/account/' + acc.id;
        if ($scope.isActive('/receive$')) link += '/receive';
        if ($scope.isActive('/send$')) link += '/send';
        return link;
    };

    $scope.forget = function (dev) {
        deviceList.forget(dev);
    };

    $scope.changeAccountLabel = function (device, account) {
        deviceList.navigateTo(device);
        promptAccountLabel(account)
            .then(function (settings) {
                var label = settings.label.trim();
                if (label.length > account.LABEL_MAX_LENGTH) {
                   label = label.slice(0, account.LABEL_MAX_LENGTH);
                }
                return device.changeAccountLabel(account, label).then(function() {return label;});
            })
            .then(
                function (label) {
                    account.setLabelInDevice(label);
                    flash.success($translate.instant('js.controllers.nav.account-label-change-successful'));
                },
                function (err) {
                    if (err) {
                        flash.error(err.message ||
                                    $translate.instant('js.controllers.nav.account-label-change-failed'));
                    }
                }
            );
    };

    function promptAccountLabel(account) {
        var scope,
            modal;

        scope = angular.extend($scope.$new(), {
            label: account.labelInDevice() || '',
            labelMaxLength: account.LABEL_MAX_LENGTH
        });

        modal = $modal.open({
            templateUrl: 'views/modal/account.label.html',
            //size: 'sm',
            windowClass: 'labelmodal',
            backdrop: 'static',
            keyboard: false,
            scope: scope,
        });
        modal.opened.then(function () {
            scope.$emit('modal.label.show');
        });
        modal.result.finally(function () {
            scope.$emit('modal.label.hide');
        });

        return modal.result;
    }

});
